<?php
/**
 * 用户表数据操作层
 * @author yangchao
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2011-01-17
 */


define("CACHE_PREFIX_mblog_cron_topic_tag","mblog_cron_topic_tag_");


class db_mblog_cron_topic_tag extends db_base {
    
	var $table_name = "mblog_cron_topic_tag";
	
	function db_mblog_cron_topic_tag()
	{
		parent::db_base();
	}
	
	/**
	 * 前台取单条信息
	 * @param $uid
	 */		 
	function get_mblog_cron_topic_tag_item_by_id($id)
	{
		$id = intval($id);
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_item_by_id-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","id='{$id}' and status=1");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_WEEK);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	/**
	 * 根据站点取出所有tag
	 * @param $uid
	 */
	function get_current_tag_list_order_by_count_mblog($p=1,$pcount=5,$topic_id="")
	{
	
		$p = intval($p);
		$pcount = intval($pcount);
	
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_list_order_by_count_want-{$p}-{$pcount}-{$topic_id}";
		$cache = $mc->get($key);
		
		if ($cache === false)
		{
			$where_ary = array();
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where_ary[] = "status = 1";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"count_mblog desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		
		return $content;
		
	}
	
	
	/*
	 * cron获取topic_lisy
	 */
	function get_mblog_topic_list_need_cron($p=1,$pcount=10,$show_types=array())
	{
		if(!$show_types)
		{
			$show_types = array();
		}
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_topic_list_need_cron-{$p}-{$pcount}-".join(",",$show_types);
		$cache = $mc->get($key);
		if ($cache === false)
		{
			foreach($show_types as $key => $value)
			{
				$show_types[$key] = intval($show_types[$key]);
			}
			
			$p = intval($p);
			$pcount = intval($pcount);
			
			$start = ($p -1 ) * $pcount;
			$where_array[] = " need_cron =1 ";
			$where_array[] = "  status = 1  ";
			if($show_types)$where_array[] = "show_type in (".join(",",$show_types).")";
			$where = join(" and ",$where_array);
			
			
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"count_mblog_newly desc",$where);
			
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_DAY);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		
		return $content;
	}
	
	
	function get_mblog_cron_topic_tag_item_by_id_with_del($id)
	{
		$id = intval($id);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_item_by_id_with_del-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","id='{$id}'");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	
	
	
	function get_mblog_cron_topic_tag_item_by_topic_with_del($topic)
	{
		$id = intval($id);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_item_by_topic-{$topic}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","topic='{$topic}'");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	
	/**
	 * 前台取单条信息
	 * @param $uid
	 */
	function get_mblog_cron_topic_tag_list_order_by_like($show_type="",$p=1,$pcount=5)
	{
		
		$p = intval($p);
		$pcount = intval($pcount);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_list_order_by_like-{$show_type}-{$p}-{$pcount}";
		$cache = $mc->get($key);
		
		if ($cache === false)
		{
			$where_ary = array();;
			if(strlen($show_type)>0)
			{
				$show_type = intval($show_type);
				$where_ary[] = "show_type = {$show_type}";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"count_mblog_newly desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		
		return $content;
		
	}
	
/**
	 * 前台取单条信息
	 * @param $uid
	 */
	function get_mblog_cron_topic_tag_list_order_by_count_want($show_type="",$p=1,$pcount=5)
	{
		$p = intval($p);
		$pcount = intval($pcount);
		
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_list_order_by_count_want-{$show_type}-{$p}-{$pcount}";
		$cache = $mc->get($key);
		
		if ($cache === false)
		{
			$where_ary = array();;
			if(strlen($show_type)>0)
			{
				$show_type = intval($show_type);
				$where_ary[] = "show_type = {$show_type}";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"count_want desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		
		return $content;
		
	}
	
	
	function get_mblog_cron_topic_tag_list_order_by_count_gonglue($show_type="",$p=1,$pcount=5)
	{
		$p = intval($p);
		$pcount = intval($pcount);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_list_order_by_count_gonglue-{$show_type}-{$p}-{$pcount}";
		$cache = $mc->get($key);
		
		if ($cache === false)
		{
			$where_ary = array();;
			if(strlen($show_type)>0)
			{
				$show_type = intval($show_type);
				$where_ary[] = "show_type = {$show_type}";
				
			}
			$where_ary[] = "status = 1 and count_gonglue > 0";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"count_gonglue desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	function get_mblog_cron_topic_tag_count_by_uid($uid="")
	{
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_count_by_uid-{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where_ary = array();;
			if(strlen($uid)>0)
			{
				$uid = intval($uid);
				$where_ary[] = "uid = {$uid}";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_listcount($this->table_name,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	function get_mblog_cron_topic_tag_list_by_uid($uid="",$p=1,$pcount=5)
	{
		$p = intval($p);
		$pcount = intval($pcount);
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_list_by_uid-{$uid}-{$p}-{$pcount}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where_ary = array();;
			if(strlen($uid)>0)
			{
				$uid = intval($uid);
				$where_ary[] = "uid = {$uid}";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	function get_mblog_cron_topic_tag_list_by_topic_id($uid="",$p=1,$pcount=5)
	{
		$p = intval($p);
		$pcount = intval($pcount);
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER_ITEM."-get_mblog_cron_topic_tag_list_by_uid-{$uid}-{$p}-{$pcount}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where_ary = array();;
			if(strlen($uid)>0)
			{
				$uid = intval($uid);
				$where_ary[] = "uid = {$uid}";
				
			}
			$where_ary[] = "status = 1";
			
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	
	function insert_mblog_cron_topic_tag($topic,$uid,$nickname)
	{
		// 插入数据
		$ary = array();

		if(strlen($topic)>0)$ary["topic"] = $topic;
		$ary["status"] = 1;
		
		$ary["show_type"] = 4;//网友游戏
		$ary["uid"] = $uid;
		$ary["nickname"] = $nickname;
		$ary["type"] = 2;//用户添加
		$ary["create_time"] = Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		$ary["update_time"] = Date::get_date_time();
		
		
		$ret = $this->insert($this->table_name, $ary);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $this->get_lastinsertid();
	}
	
	
	
	function update_mblog_cron_topic_tag($id,$uid="",$nickname="",$need_cron="")
	{
		// 插入数据
		$ary = array();

		if(strlen($nickname)>0)$ary["nickname"] = $nickname;
		
		if(strlen($uid)>0)
		{
			$ary["uid"] = $uid;
			if($uid ==0)
			{
				$nickname ="";
			}
		}
		
		if(strlen($need_cron)>0)$ary["need_cron"] = $need_cron;
		$ary["update_time"] = Date::get_date_time();
		
		$id = intval($id);
		$where = "id=$id";
		$ret = $this->update($this->table_name, $ary,$where);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $ret;
	}
	
	
	function update_mblog_cron_topic_tag_count($id,$count_love="",$count_mblog_newly="",$count_mblog_last_newly="")
	{
		// 插入数据
		$ary = array();

		if(strlen($count_love)>0)
		{
			$ary["count_love"] = "count_love+1";
		}
		
		if($count_mblog_newly)$ary["count_mblog_newly"] = $count_mblog_newly;
		if($count_mblog_last_newly)$ary["count_mblog_last_newly"] = $count_mblog_last_newly;
		
		
		$where = "id=$id";
		$ret = $this->update($this->table_name, $ary,$where);
		


		return $ret;
	}
	
	
	
	####################后台程序
	
	
	
	function admin_insert_mblog_cron_topic_tag($topic_id,$uid,$nickname,$tag_name,$alias,$black_word)
	{
		// 插入数据
		$ary = array();

		$ary["topic_id"] = $topic_id;
		$ary["status"] = 1;
		$ary["uid"] = $uid;
		$ary["nickname"] = $nickname;
		$ary["tag_name"] = $tag_name;
		$ary["alias"] = $alias;
		$ary["black_word"] = $black_word;
		
		
		$ary["create_time"] = Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		$ary["update_time"] = Date::get_date_time();
		
		
		$ret = $this->insert($this->table_name, $ary);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $this->get_lastinsertid();
	}
	
	function admin_update_mblog_cron_topic_tag($id,$topic_id="",$status="",$tag_name="",$alias="",$black_word="")
	{
		// 插入数据
		$ary = array();

		if(strlen($topic_id)>0)$ary["topic_id"] = $topic_id;
		if(strlen($status)>0)$ary["status"] = $status;
		if(strlen($tag_name)>0)$ary["tag_name"] = $tag_name;
		$ary["alias"] = $alias;
		$ary["black_word"] = $black_word;
		$where = "id=$id";
		$ret = $this->update($this->table_name, $ary,$where);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $this->get_lastinsertid();
	}
	
	
	function admin_get_mblog_cron_topic_tag_item_by_id($id)
	{
		$content = $this->get_alllist($this->table_name,1,1,"","id='{$id}'");
		
		return $content[0];
		
	}
	
	
	
	function admin_get_mblog_cron_topic_tag_list_by_search($p=1,$pcount=10,$type="",$status="",$topic_id="",$need_cron="",$topic="")
	{
		
		if($topic_id)$where_array[] = "topic_id = {$topic_id}";
		if($type)$where_array[] = "type = {$type}";
		if(strlen($status)>0)$where_array[] = "status = {$status}";
		if(strlen($need_cron)>0)$where_array[] = "need_cron = {$need_cron}";
		if(strlen($topic)>0)$where_array[] = "topic like '%{$topic}%'";
		
		if($where_array)$where = join(" and ",$where_array);
		
		$content = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
		
		return $content;
	}
	
	
	function admin_get_mblog_cron_topic_tag_count_by_search($type="",$status="",$topic_id="",$need_cron="",$topic="")
	{
		
		if($topic_id)$where_array[] = "topic_id = {$topic_id}";
		if($type)$where_array[] = "type = {$type}";
		if(strlen($status)>0)$where_array[] = "status = {$status}";
		if(strlen($need_cron)>0)$where_array[] = "need_cron = {$need_cron}";
		if(strlen($topic)>0)$where_array[] = "topic like '%{$topic}%'";
		
		
		if($where_array)$where = join(" and ",$where_array);
		
		$content = $this->get_listcount($this->table_name,$where);
	
		return $content;
	}
	
	
###########################

	
	
	
	/**
	 * cron 获取food_tag
	 */
	function cron_get_mblog_topic_count($start_id)
	{
		global $config;
		
		$start_id = intval($start_id);
		
		$where=" id > $start_id  and status = 1 " ;
		
		$data = $this->get_listcount($this->table_name, $where);
		
		return $data;
	}
	
	
	/*
	 * cron获取topic_lisy
	 */
	function cron_get_mblog_topic_list($start_id=0,$p=1,$pcount=10)
	{
		global $config;
		$start_id = intval($start_id);
		$p = intval($p);
		$pcount = intval($pcount);
		
		$where=" id > $start_id  and status = 1 " ;
		
		$data = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
		
		return $data;
	}
}



